﻿

using Framework.Utils;

namespace Framework.Components
{
    /// <summary>
    /// 结果接口
    /// </summary>
    public interface IResult
    {
        /// <summary>
        /// 结果
        /// </summary>
        int Code { get; }

        /// <summary>
        /// 消息
        /// </summary>
        string Msg { get; }

        /// <summary>
        /// 是否成功
        /// </summary>
        bool IsSuccess { get; }
    }

    /// <summary>
    /// 结果
    /// </summary>
    public class Result : IResult
    {
        /// <summary>
        /// 成功
        /// </summary>
        public const int SUCCESS = 1;

        /// <summary>
        /// 失败
        /// </summary>
        public const int ERROR = 0;

        /// <summary>
        /// 结果
        /// </summary>
        public int Code { get; set; }

        /// <summary>
        /// 消息
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess { get { return Code > 0; } }


        /// <summary>
        /// 创建
        /// </summary>
        public static R Create<R>(int code, string msg) where R : Result, new()
        {
            R result = new R();
            result.Code = code;
            result.Msg = msg;
            return result;
        }

        /// <summary>
        /// 创建
        /// </summary>
        public static Result Create(int code, string msg)
        {
            return Create<Result>(code, msg);
        }
        
        /// <summary>
        /// 创建成功消息
        /// </summary>
        /// <returns></returns>
        public static Result Success()
        {
            return Create(SUCCESS, null);
        }

        /// <summary>
        /// 创建成功消息
        /// </summary>
        /// <returns></returns>
        public static R Success<R>() where R : Result, new()
        {
            return Create<R>(SUCCESS, null);
        }

        /// <summary>
        /// 创建失败消息
        /// </summary>
        /// <returns></returns>
        public static Result Error(string msg)
        {
            return Error<Result>(msg);
        }

        /// <summary>
        /// 创建失败消息
        /// </summary>
        public static R Error<R>(string msg) where R : Result, new()
        {
            return Create<R>(ERROR, msg);
        }


        public override string ToString()
        {
            return StringUtils.ToString(this);
        }
    }

    /// <summary>
    /// 数据结果
    /// </summary>
    public class DataResult<T> : Result
    {
        /// <summary>
        /// 数据体
        /// </summary>
        public T Data { get; set; }

        /// <summary>
        /// 获取数据
        /// </summary>
        public T GetData() { return Data; }


        /// <summary>
        /// 创建
        /// </summary>
        public static DataResult<T> Create(int code, string msg, T data)
        {
            var result = Create(code, msg);
            result.Data = data;
            return result;
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="code"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public static new DataResult<T> Create(int code, string msg)
        {
            var result = new DataResult<T>();
            result.Code = code;
            result.Msg = msg;
            return result;
        }

        /// <summary>
        /// 创建成功消息
        /// </summary>
        /// <returns></returns>
        public static DataResult<T> Success(T data)
        {
            return Create(SUCCESS, null, data);
        }

        /// <summary>
        /// 创建失败消息
        /// </summary>
        /// <returns></returns>
        public static new DataResult<T> Error(string msg)
        {
            return Create(ERROR, msg);
        }
    }


}
